/**
 * 
 */
package com.feib.stms.action;

import java.util.Date;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

import com.feib.stms.model.ConfirmRule;
import com.feib.stms.model.User;
import com.feib.stms.service.ConfirmRuleService;
import com.feib.stms.util.UserUtil;

/**
 * @title (#)Stms9008ActionBean.java<br>
 * @description     券商流程設定 <br>
 * 設定券商流程參數，包含放行認證方式、審查通過門檻人數、放行通過門檻人數等等。<br/>
 * 業務規則說明:
 * <ol>
 * <li>審查通過門檻人數若為0，代表不需審查直達進入放行作業。</li>
 * <li>放行通過門檻人數不能為0，也就是一定要經過放行作業。</li>
 * </ol>
 * @author Jeff Gu
 */
public class Stms9008ActionBean extends BaseActionBean {



	private static final long serialVersionUID = 3837838881421501841L;

	/* 基本頁面 此功能不需轉頁 */
	private static final String DEFAULT_JSP = "/WEB-INF/views/stms9008/stms9008.jsp";

	/**
	 * 查詢結果(單筆),與修改
	 */
	private  ConfirmRule  queryConfirmRule;


	/**
	 * 使用者
	 * */
	private User user;


	

	/**
	 * 錯誤訊息
	 */
	private String message;



	@SpringBean("confirmRuleService")
	private ConfirmRuleService confirmRuleService;



	@DontBind
	@DefaultHandler
	public Resolution doDefault(){

		logger.debug("Enter doDefault mehtod.");
		return doQuery(); 
	}


	public Resolution doQuery()
	{
		logger.debug("Enter doQuery mehtod.");	
		user=UserUtil.getUser();		
		queryConfirmRule=this.confirmRuleService.findConfirmRuleEnabled(user.getGroup());
		
		if(null==queryConfirmRule){
			ConfirmRule confirmRule= new ConfirmRule();
			confirmRule.setCheckCount(1);
			confirmRule.setConfirmCount(1);
			confirmRule.setConfirmType(0);
			queryConfirmRule=confirmRule;
		}
		

		return new ForwardResolution(DEFAULT_JSP); 
	}

	/**
	 * 儲存業者資訊(修改)
	 * 
	 * @return
	 */
	public Resolution doSaveConfirmRule(){

		logger.debug("Enter doSaveConfirmRule mehtod.");
		
		user=UserUtil.getUser();		
		
		/* 更新人員;GROUP_NO+"_"+USER_ID */
		String editPerson=user.getGroup().getGroupNo()+"_"+user.getUserId();
		Date dNow= new Date();
		queryConfirmRule.setGroup(user.getGroup());
		queryConfirmRule.setCreatedBy(editPerson);
		queryConfirmRule.setCreatedDate(dNow);
		queryConfirmRule.setUpdatedBy(editPerson);
		queryConfirmRule.setUpdatedDate(dNow);
		
		int insertCount=0;
		
		
		/* 先將該GROUP所有confirmRule 設為 DISABLE */
		insertCount=confirmRuleService.disableConfirmRule(queryConfirmRule);
		
		
		if(insertCount <= 0){
			message = "更新失敗!";
			return doQuery();
		}	
	
		/* 再新增一筆新的 */
		insertCount= confirmRuleService.saveConfirmRule(queryConfirmRule);
		
		message = insertCount > 0 ? "更新成功！": "更新失敗!";
		if(insertCount > 0){

			return doQuery();
		}	
		return new ForwardResolution(DEFAULT_JSP); 
	}


	/**
	 * @param queryConfirmRule 的設定的 queryConfirmRule
	 */
	public void setQueryConfirmRule(ConfirmRule queryConfirmRule) {
		this.queryConfirmRule = queryConfirmRule;
	}



	/**
	 * @return queryConfirmRule
	 */
	public ConfirmRule getQueryConfirmRule() {
		return queryConfirmRule;
	}



	/**
	 * @param user 的設定的 user
	 */
	public void setUser(User user) {
		this.user = user;
	}



	/**
	 * @return user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param message 的設定的 message
	 */
	public void setMessage(String message) {
		this.message = message;
	}



	/**
	 * @return message
	 */
	public String getMessage() {
		return message;
	}


}
